<?php
/**
 * Form_Form
 *
 * класс Form_Form позволяет работать с формами
 * @author Дмитрий Кузьма <kouzma.d@gmail.com>
 * @version 2.1
 */

class Form_Form extends Form_Fields {

  protected $_id;
  protected $_action;
  protected $_target;
  protected $_enctype;
  protected $_successMessage;
  protected $_submitButtonName = null;
  protected $_isFormSent =false;


	public function __construct($arrSettings = array(), $objAR = null) {
  	parent::__construct($arrSettings, $objAR);
  	if ($_POST) {
  		if ($this->_submitButtonName)  {
  			if (isset($_POST[$this->_submitButtonName]) || isset($_POST[$this->_submitButtonName . '_x']) || isset($_POST[$this->_objARName][$this->_submitButtonName]) || isset($_POST[$this->_objARName][$this->_submitButtonName . '_x'])) {
  				$this->_isFormSent = true;

  			}
  		}
  		else
  		  $this->_isFormSent = true;
  	}
  }

  /**
	 * Метод устанавливает свойство
	 *
	 * @param string $settingName
	 * @param mixed $settingValue
	 */
	public function setSetting($settingName, $settingValue, $additionalAllowedSettings = null) {
    switch ($settingName) {
    	case 'id':
    		$this->setId($settingValue);
    		break;
    	case 'action':
    		$this->setAction($settingValue);
    		break;
    	case 'target':
    		$this->setTarget($settingValue);
    		break;
    	case 'successMessage':
    		$this->setSuccessMessage($settingValue);
    		break;
    	case 'enctype':
    		$this->setEnctype($settingValue);
    		break;
    	case 'submitButtonName':
    		$this->setSubmitButtonName($settingValue);
    		break;
    	default:
    	  parent::setSetting($settingName, $settingValue, '"action", "enctype", "successMessage"');
			  break;
    }
	}
	/**
	 * Устанавливает свойство action
	 *
	 * @param string $action
	 */
	private function setAction($action) {
		$this->_action = $action;
	}
	/**
	 * Устанавливает свойство id
	 *
	 * @param string $id
	 */
	private function setId($id) {
		$this->_id = $id;
	}
	/**
	 * Устанавливает назначение сабмита формы
	 *
	 * @param string $target
	 */
	private function setTarget($target) {
		$this->_target = $target;
	}

	/**
	 * Устанавливает свойство enctype
	 *
	 * @param string $enctype
	 */
	private function setEnctype($enctype) {
		$this->_enctype = $enctype;
	}
	/**
	 * Устанавливает сообщение об успехе
	 *
	 * @param string $successMessage
	 */
	private function setSuccessMessage($successMessage) {
		$this->_successMessage = $successMessage;
	}
	/**
	 * Устанавливает name кнопки отправки формы
	 *
	 * @param string submitButtonName
	 */
	private function setSubmitButtonName($submitButtonName) {
		$this->_submitButtonName = $submitButtonName;
	}

	/**
	 * Выводит тег <form> и иже с ним
	 *
	 */
	public function show() {
	  if ($this->_errorIfExists) {
		  if (Exception_ActiveRecord::isExceptionOfModelExists(get_class($this->_objAR))) {
		  	View_User::showUserMessage(Exception_ActiveRecord::getExceptionsOfModel(get_class($this->_objAR)));
		  }
		}
		if (isset($this->_successMessage) && $this->_isFormSent) {
		  if (!Exception_ActiveRecord::isExceptionOfModelExists(get_class($this->_objAR))) View_User::showUserMessage($this->_successMessage, 1);
		}
		echo '<form';
		if (isset($this->_id)) echo ' id="' . $this->_id . '"';
		if (isset($this->_action)) echo ' action="' . $this->_action . '"';
		if (isset($this->_target)) echo ' target="' . $this->_target . '"';
		if (isset($this->_method)) echo ' method="' . $this->_method . '"';
		if (isset($this->_enctype)) echo ' enctype="' . $this->_enctype . '"';
		if (isset($this->_class)) echo ' class="' . $this->_class . '"';
		else echo ' class="standard"';
		if (isset($this->_style)) echo ' style="' . $this->_style . '"';
		if (isset($this->_js)) echo ' ' . $this->_js;
		echo '>';
		if (isset($this->_objAR) && isset($this->_objAR->id) && $this->_objAR->id)
		  $this->hiddenField(array('value' => $this->_objAR->id), $this->_objAR->pkName);
	}
	/**
	 * Выводит тег </form> и иже с ним
	 *
	 */
	public function close() {
		echo '</form>';
	}
}